# See for codes
# http://www.djangoproject.com/documentation/models/test_client/
from datetime import datetime, timedelta

from django.test import TestCase, Client
from django.contrib.auth.models import User
from django.utils.http import urlquote

from pypsqueak.group.models import *
from pypsqueak.group.forms import *


class GroupFormTestCase(TestCase):
	""" Test form for groups. """

	def setUp(self):

		# Create test users.
		self.staff_user = User(
			username = 'staff',
			last_name='last_name',
			first_name='first_name',
			is_staff=True
			)
		self.staff_user.set_password( 'password' )
		self.staff_user.save()

		self.other_user = User(
			username = 'other',
			last_name='last_name',
			first_name='first_name',
			)
		self.other_user.set_password( 'password' )
		self.other_user.save()


		# Create test group
		self.group = Group()
		self.group.title = 'group'
		self.group.slug = 'slug'
		self.group.save()

		# Create test assignment.
		self.assignment = Assignment()
		self.assignment.title = 'Assignment'
		self.assignment.description = "Description"
		self.assignment.group = self.group
		self.assignment.save()

	def testGroup_AnonUserFailure(self):
		""" Test Anonymous user group rights. """

		c = Client()
		self.view(c,False)


	def testGroup_UserFailure(self):
		""" Test normal user group rights. """

		c = Client()
		response = c.login(username=self.other_user.username, password='password')
		self.assertEquals( response, True)

		self.view(c,False)

	def testGroup_StaffSuccess(self):
		""" Test staff user group rights. """
		c = Client()
		response = c.login(username=self.staff_user.username, password='password')
		self.assertEquals( response, True)

		self.view(c,True)


	def view(self, client, result):
		# Expected results

		if result:
			equal = self.assertEqual
			contains = self.assertContains
		else:
			equal = self.assertNotEqual
			contains = self.assertNotContains

		#### GROUP

		# Does the main group url work?
		response = client.get( self.group.get_absolute_url() )
		self.assertEqual( response.status_code, 200 )

		# Does the group list view has 'add' field?
		response = client.get( '/group/' )
		contains( response, Group.get_insert_url())

		# Make sure that the insert view throws http error
		response = client.get( self.group.get_insert_url() )
		equal( response.status_code, 200 )
		if response.status_code == 200:
			contains(response, ChromeModelForm.insert)

		# Test detail view to ensure links are/aren't shown.
		response = client.get(self.group.get_absolute_url() )
		contains(response, self.group.get_update_url())
		contains(response, self.group.get_delete_url())

		# Test update view throwing http error or redirect
		response = client.get( self.group.get_update_url() )
		equal( response.status_code, 200 )
		if response.status_code == 200:
			contains(response, ChromeModelForm.update )

		# Test delete view throwing http error or redirect
		response = client.get( self.group.get_delete_url() )
		equal( response.status_code, 200 )
		if response.status_code == 200:
			contains(response, ChromeModelForm.delete )

		#### ASSIGNMENTS

		# Make sure that the insert view throws http error
		response = client.get( self.assignment.get_insert_url() )
		equal( response.status_code, 200 )
		if response.status_code == 200:
			contains(response, ChromeModelForm.insert )

		# Test detail view to ensure links are/aren't shown.
		response = client.get(self.assignment.get_absolute_url() )
		contains(response, self.assignment.get_update_url())
		contains(response, self.assignment.get_delete_url())

		# Test update view throwing http error or redirect
		response = client.get( self.assignment.get_update_url() )
		equal( response.status_code, 200 )
		if response.status_code == 200:
			contains(response, ChromeModelForm.update )

		# Test delete view throwing http error or redirect
		response = client.get( self.assignment.get_delete_url() )
		equal( response.status_code, 200 )
		if response.status_code == 200:
			contains(response, ChromeModelForm.delete )


	def tearDown(self):
		self.assignment.delete()
		self.group.delete()
		self.other_user.delete()
		self.staff_user.delete()
